package org.example.lease.custom.converters;

import org.example.common.exception.TypeConvertException;
import org.example.lease.model.enums.BaseStatus;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Arrays;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/25 20:58
 */
@Component
@Slf4j
public class StringToBaseStatusConverter implements Converter<String, BaseStatus> {
    @Override
    public BaseStatus convert(@NotNull String source) {
        Integer code = Integer.parseInt(source);
        return Arrays.stream(BaseStatus.values())
                .filter(item -> code.equals(item.getCode()))
                .findFirst().orElseThrow(() -> new TypeConvertException("类型转换异常"));
    }
}
